#include "wuPapallonaManager.h"

wuPapallonaManager::wuPapallonaManager()
{
}

wuPapallonaManager::~wuPapallonaManager()
{
}

void wuPapallonaManager::setup(int _vw, int _vh)
{
    wuBaseManager::setup(_vw,_vh);
    vPriority = 9;
}

bool wuPapallonaManager::capture(ofImage *currImg, ofImage *bgImg, int x_offset)
{
	bool bCambiazo = true;

	wuBaseManager::capture(currImg, bgImg, x_offset);

	return bCambiazo;
}

void wuPapallonaManager::addBitxo(ofImage *_skin, ofRectangle rect, vector <ofPoint> * _pts)
{
	wuPapallona papallona;
	papallona.setup(_skin, rect);
	papallones.push_back(papallona);
}

void wuPapallonaManager::update()
{
        for (int i = 0; i < papallones.size(); i++)
            papallones[i].update();

		for (int i = papallones.size()-1; i >= 0; i--)
	    {
			// destroy bitxos que marxen fora de la pantalla
			if (papallones[i].getPosition().y > vh || papallones[i].getPosition().y < 0 || papallones[i].getPosition().x > vw || papallones[i].getPosition().x < 0)
				papallones.erase(papallones.begin()+i);
		}

}

void wuPapallonaManager::draw(bool bDrawLines, int offX)
{
        for (int i = 0; i < papallones.size(); i++)
            papallones[i].draw(bDrawLines, offX);
}

void wuPapallonaManager::drawSelected(bool bDrawLines, int offX)
{
    if(selection.width < vw || selection.height < vh)
    {

        for (int i = papallones.size()-1; i >= 0; i--)
        {
            bool condi = (papallones[i].getPosition().y > selection.y) && (papallones[i].getPosition().y < selection.y + selection.height) &&
             (papallones[i].getPosition().x > selection.x + x_scroll) && (papallones[i].getPosition().x < selection.x + x_scroll + selection.width);

            if (condi)
                papallones[i].draw(bDrawLines, offX);
        }
    }else
    {
        for (int i = 0; i < papallones.size(); i++)
            papallones[i].draw(bDrawLines, offX);
    }
}

void wuPapallonaManager::clear()
{
    if(selection.width < vw || selection.height < vh)
    {

        for (int i = papallones.size()-1; i >= 0; i--)
        {
            bool condi = (papallones[i].getPosition().y > selection.y) && (papallones[i].getPosition().y < selection.y + selection.height) &&
             (papallones[i].getPosition().x > selection.x + x_scroll) && (papallones[i].getPosition().x < selection.x + x_scroll + selection.width);

            if (condi)
            {
                papallones[i].clear();
                papallones.erase(papallones.begin()+i);
            }
        }
    }else
    {
        for (int i = 0; i < papallones.size(); i++)
            papallones[i].clear();
        papallones.clear();
    }

}

